package org.minuframework.json;

import java.lang.reflect.Type;

import org.json.JSONObject;
import org.minuframework.exception.ExceptionFailed;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Deprecated
public class GSONU{
    
    // ------------------------------------------------------------------------
    // ************************************************************************
    // constants
    
    public static final String DATEFORMATE = "yyyy-MM-dd HH:mm:ss";
    
    // ------------------------------------------------------------------------
    // ************************************************************************
    // public methods
    
    // ------------------------------------------------------------------------
    public static String ToGSONString(Object object){
        Gson gson = new GsonBuilder().setDateFormat(DATEFORMATE).create();
        
        return gson.toJson(object);
    }
    
    // ------------------------------------------------------------------------
    public static <T> T FromGSONString(String jsonString, Class<T> classOfT){
        Gson gson = new GsonBuilder().setDateFormat(DATEFORMATE).create();
        
        return (T) gson.fromJson(jsonString, classOfT);
    }
    
    @SuppressWarnings("unchecked")
    public static <T> T FromGSONString(String jsonString, Type typeOfT){
        Gson gson = new GsonBuilder().setDateFormat(DATEFORMATE).create();
        
        return (T) gson.fromJson(jsonString, typeOfT);
    }
    
    // ------------------------------------------------------------------------
    public static JSONObject ToJSONObject(Object object){
        String jsonString = ToGSONString(object);
        
        if (null == jsonString) { return null; }
        
        try {
            return JSONU.ToJSONObject(jsonString);
        } catch (ExceptionFailed e) {}
        
        return null;
    }
    
    public static Object DuplicateObject(Object object){
        return new Gson().fromJson(new Gson().toJson(object), object.getClass());
    }
    
    public static <T> T BuildObjectFormJson(Gson gson, String jsonString, Class<T> classOfT){
        if (null == jsonString) {
            try {
                return (T) classOfT.newInstance();
            } catch (Exception e) {
                return null;
            }
        }
        
        T result = gson.fromJson(jsonString, classOfT);
        return result;
    }
    
}
